<?php
/**
 * Created by JetBrains PhpStorm.
 * User: zhouyang
 * Date: 13-9-06
 * Time: 下午22:31
 * To change this template use File | Settings | File Templates.
 */
class AdsDB  extends DBReader{

    //根据ad_id 获取ad 信息
    public function get($ad_id){

        $sql = 'select ad_id, ad_type_id, title, link, picture, sort, user_id, start_time, end_time, status from ads where ad_id = :ad_id';

        return $this->db()->getRow($sql,array('ad_id'=>$ad_id));
    }

    //获取推广信息 列表<带分页>
    public function getAdList($curr_page,$per_page,$filter = array()){
        $where = ' 1=1 ';
        $bindarr = array();

        if(isset($filter['title'])){
            $where .= ' and title like :title';
            $bindarr['title'] = '%'.$filter['title'].'%';
        }

        if(isset($filter['ad_type_id'])){
            $where .= ' and ad_type_id = :ad_type_id';
            $bindarr['ad_type_id'] = $filter['ad_type_id'];
        }

        if(isset($filter['start_time'])){
            $where .= ' and start_time >= :start_time';
            $bindarr['start_time'] = $filter['start_time'];
        }

        if(isset($filter['end_time'])){
            $where .= ' and end_time <= :end_time';
            $bindarr['end_time'] = $filter['end_time'];
        }

        $ret = $this->pager($curr_page, $per_page)
            ->ct('select count(ad_id) from ads where ' . $where , $bindarr)
            ->rs('select ad_id, ad_type_id, title, link, picture, sort, user_id, start_time, end_time, status from ads where '.$where.' order by ad_id desc',$bindarr);

        return $ret;
    }

    //添加推广信息
    public function addAd($ad_type_id, $title,$summary = null, $link, $picture, $sort, $user_id, $start_time, $end_time, $status,$note = null){
        $sql = "insert into ads(ad_type_id, title, summary, link, picture, sort, user_id, start_time, end_time, status, note) values(:ad_type_id, :title, :summary, :link, :picture, :sort, :user_id, :start_time, :end_time, :status, :note)";

        $bindarr = array(
            'ad_type_id'=>$ad_type_id,
            'title'=>$title,
            'summary'=>$summary,
            'link'=>$link,
            'picture'=>$picture,
            'sort'=>$sort,
            'user_id'=>$user_id,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'status'=>$status,
            'note'=>$note
        );
        $this->db()->execute($sql,$bindarr);
        return $this->db()->lastInsertId();
    }

    //删除推广信息
    public function delAd($ad_id){

        $sql = "delete from ads where ad_id = :ad_id";

        $bindarr = array(
            'ad_id'=>$ad_id,
        );
        $rs = $this->db()->execute($sql,$bindarr);

        return $rs->rowCount();
    }

    //修改推广信息
    public function updateAd($ad_id, $ad_type_id, $title, $link, $picture, $sort, $user_id, $start_time, $end_time, $status){
        $sql = "update ads set ad_type_id = :ad_type_id,title = :title,link = :link,picture = :picture,sort = :sort,user_id = :user_id,start_time = :start_time,end_time = :end_time,status = :status where ad_id = :ad_id";
        $bindarr = array(
            'ad_id' => $ad_id,
            'ad_type_id'=>$ad_type_id,
            'title'=>$title,
            'link'=>$link,
            'picture'=>$picture,
            'sort'=>$sort,
            'user_id'=>$user_id,
            'start_time'=>$start_time,
            'end_time'=>$end_time,
            'status'=>$status,
        );
        return $this->db()->execute($sql,$bindarr);
    }
}